/*
 * MenuSistema.java
 *
 * Created on 04/07/2007, 23:02:54
 *
 */
package br.com.grillo.jturbonote;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 *
 * @author grillo
 */
public class MenuSistema extends JFrame implements ActionListener {

    private SystemTray tray = SystemTray.getSystemTray();
    private TrayIcon trayIcon;
    private List<FrameNota> notas;
    private static MenuSistema _instance;
    private static final Object lock = new Object();

    private MenuSistema() {
        notas = new ArrayList<FrameNota>();
    }

    public static MenuSistema getInstance() {
        if (_instance == null) {
            synchronized (lock) {
                _instance = new MenuSistema();
            }
        }
        return _instance;
    }

    protected void init() throws IOException {

        PopupMenu popUp = new PopupMenu();
        BufferedImage imagem = ImageIO.read(getClass().getResource("/br/com/grillo/jturbonote/resource/jturbonote.ico"));
        trayIcon = new TrayIcon(imagem);
        trayIcon.setToolTip("JTurboNote");

        MenuItem conf = new MenuItem("Configuracao");
        conf.addActionListener(this);
        conf.setActionCommand("conf");

        MenuItem showAll = new MenuItem("Mostrar todas");
        showAll.addActionListener(this);
        showAll.setActionCommand("showAll");

        MenuItem novaNota = new MenuItem("Nova Nota");
        novaNota.setActionCommand("nova");
        novaNota.addActionListener(this);

        MenuItem fechar = new MenuItem("Fechar");
        fechar.setActionCommand("fechar");
        fechar.addActionListener(this);

        popUp.add(conf);
        popUp.add(showAll);
        popUp.add(novaNota);
        popUp.addSeparator();
        popUp.add(fechar);

        trayIcon.setPopupMenu(popUp);

        trayIcon.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                new FrameNota().setVisible(true);
            }
        });
        try {
            tray.add(trayIcon);
        } catch (AWTException ex) {
            System.out.println("erro ao carregar o tray");
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("conf")) {
            new FrameConfiguracao(this, true).setVisible(true);
        } else if (e.getActionCommand().equals("nova")) {
            new FrameNota().setVisible(true);
        } else if (e.getActionCommand().equals("showAll")) {
            for (FrameNota fn : notas) {
                fn.setVisible(true);
            }
        } else if (e.getActionCommand().equals("fechar")) {
            tray.remove(trayIcon);
            System.exit(0);
        }
    }

    public void removeNota(FrameNota fNota) {
        notas.remove(fNota);
        //trayIcon.setToolTip("JTurboNote - " + notas.size() + " nota(s)");
    }

    public void addNota(FrameNota fNota) {
        notas.add(fNota);
        //trayIcon.setToolTip("JTurboNote - " + notas.size() + " nota(s)");
    }
}
